package com.czk.springcloud.controller;

import com.czk.springcloud.entity.CommonResult;
import com.czk.springcloud.entity.Payment;
import com.czk.springcloud.service.PaymentFeignService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Author:ChenZhangKun
 * @Date: 2020/12/18 14:19
 */
@RestController
@Slf4j
public class OrderFeignController {
    @Resource
    private PaymentFeignService paymentFeignService;

    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) {
        log.info("调用feign客户端实现负载均衡");
        return paymentFeignService.getPaymentById(id);
    }

    @GetMapping("/consumer/feign/timeout")
    public String paymentFeignTimeout() {
        log.info("feign超时演示");
        // openFeign默认等待时间是一秒钟，超时则会抛出异常
        return paymentFeignService.paymentFeignTimeout();
    }

    @GetMapping("/test/feign")
    public Payment testFeign() {
        Payment payment = paymentFeignService.payment();
        return payment;
    }

    @GetMapping("/error/test")
    public Payment testError() {
        Payment payment = paymentFeignService.selectByCode("czk");
        return payment;
    }
}
